##FIGURE 2

## 
## R Version: R version 3.2.4 (2016-03-1)
##

##LOADING PACKAGES
library(leiv) # Version: 2.0-7

##SET WORKING DIRECTORY
setwd('/workspace/fed_courts/scaling_paper/')

##LOADING DATASET
fjc <- read.csv('replication_materials/data/bonica_sen_fed_judges.csv')
 
##RESCALING JCS SCORES -- ERRORS-IN-VARIABLES REGRESSION MODEL 
lm.out <- leiv(dime.cfscore~jcs.score.dw,data=fjc,abs.tol=1e-10)
fjc$jcs.score.dw <- lm.out@intercept + lm.out@slope*fjc$jcs.score.dw

##SCATTER PLOT MATRIX
panel.cor <- function(x, y,col=col, digits = 2, prefix = "r = ", cex.cor, ...){

    prefix <- ''
    usr <- par("usr"); on.exit(par(usr))
    par(usr = c(0, 1, 0, 1))
    r <- round(abs(cor(x, y,use='complete.obs')),2)
    txt <- format(c(r, 0.123456789), digits = digits)[1]
    txt <- paste0('All: ',prefix, txt)
    rdem <- round(abs(cor(x[col=='black'], y[col=='black'],use='complete.obs')),2)
    txtdem <- format(c(rdem, 0.123456789), digits = digits)[1]
    txtdem <- paste0('Dem: ',prefix, txtdem)
    rrep <- round(abs(cor(x[col=='darkgrey'], y[col=='darkgrey'],use='complete.obs')),2)
    txtrep <- format(c(rrep, 0.123456789), digits = digits)[1]
    txtrep <- paste0('Rep: ',prefix, txtrep)
    if(missing(cex.cor)) cex.cor <- 0.8/strwidth(txt)
    text(0.5, 0.7, txt, cex = 2)
    text(0.5, 0.5, txtdem, cex = 2)##,col='black')
    text(0.5, 0.3, txtrep, cex = 2)##,col='darkgrey')
}

panel.hist <- function(x, ...) {
    usr <- par("usr")
    par(new = TRUE,c(usr[1:2], 0, 1.5))
    yul <- max(max(density(na.omit(x[pcol=='darkgrey']),bw = "SJ",adjust=2)$y),
               max(density(na.omit(x[pcol=='black']),bw = "SJ",adjust=2)$y))
    pc <- factor(pcol)
    densityPlot(x~pc,
                adjust=2,
                rug=FALSE,
                grid=FALSE,
                lty=c(1,1),
                legend.location = NA,
                ylim=c(0,yul*1.5),
                xaxt='n',yaxt='n',
                col=c('blue','red'))

}

fjc$pcol  <- ifelse(fjc$party.affiliation.of.president=='Democratic','black','darkgrey')
fjc$pletter<- ifelse(fjc$party.affiliation.of.president=='Democratic','D','R')
syear <- 1980
use <-  fjc$enter.year >= syear
 
use <- !duplicated(fjc$fjc.judge.idno)
cmat <- data.frame(fjc$dime.cfscore,
                   fjc$jcs.score.dw,
                   fjc$overimputed.dime.cfscore)[use,]
pcol <- fjc$pcol[use]
pletter <- fjc$pletter[use]
colnames(cmat) <- c('Contributor DIME scores','Judicial Common-Space Scores','Overimputed Values')

##PLOT PDF 
pdf('replication_materials/figures/figure_2_scatterplot_matrix.pdf',width = 10,height= 10)
print(pairs(cmat,col=pcol,
            xlim=c(-1.60,1.60),ylim=c(-1.60,1.60),
            upper.panel = panel.cor,
            pch=pletter,alpha=.4))
dev.off()

##PLOT EPS 
setEPS()
postscript('replication_materials/figures/figure_2_scatterplot_matrix.eps',width = 10,height= 10)
print(pairs(cmat,col=pcol,
            xlim=c(-1.60,1.60),ylim=c(-1.60,1.60),
            upper.panel = panel.cor,
            pch=pletter,alpha=.4))
dev.off()




